<?php
/**
 * Created by PhpStorm.
 * User: Vova
 * Date: 9/28/14
 * Time: 9:59 PM
 */

//namespace controllers;

include_once("Controller.class.php");
require_once("C:/xampp/htdocs/door-market/php/models/Item_model.class.php");
require_once("C:/xampp/htdocs/door-market/php/views/Item_view.php");


require_once("C:/xampp/htdocs/door-market/php/classes/Host_config.class.php");

class Item_controller extends Controller{

    private $item_no = 0;

    function __construct()
    {
        $this->model = new Item_model();
        $this->view = new  Item_view();
    }
    public function doors($sub_category,$args){
        $category = 'doors';
        $this->action($category,$sub_category,$args);
    }
    public function beds($sub_category,$args){
        $category = 'beds';
        $this->action($category,$sub_category,$args);

    }
    public function stairs($sub_category,$args){
        $category = 'stairs';
        $this->action($category,$sub_category,$args);

    }
    public function fires($sub_category,$args){
        $category = 'fires';
        $this->action($category,$sub_category,$args);
    }
    public function tables($sub_category,$args){
        $category = 'tables';
        $this->action($category,$sub_category,$args);
    }

    public  function action($category,$sub_category,$args){
        $this->parse_args($args);

        //$item_no = 380;




        $category_list = array(
            "doors" => array("title" => 'Двері',"class" => "menu_category_item", "link" => "",
                "style" =>"",
                  "sub"   => array(
                           "all" => array("title" => 'Всі двері',"class" => "active_sub_category", "link" => "", "items_count" => 381),
                      "entrance" => array("title" => 'Вхідні',"class" => "sub_category", "link" => "", "items_count" => 381),
                         "rooms" => array("title" => 'Міжкімнатні',"class" => "sub_category", "link" => "", "items_count" => 381),
                         "glass" => array("title" => 'Під скло',"class" => "sub_category", "link" => "", "items_count" => 381),
                            "wc" => array("title" => 'Ванна-туалет',"class" => "sub_category", "link" => "", "items_count" => 381)
                                  )
                 ),
            "stairs" => array("title" => 'Сходи',"class" => "menu_category_item", "link" => "",
                "style" =>"",
                "sub"   => array(
                           "all" => array("title" => 'Всі сходи',"class" => "active_sub_category", "link" => "", "items_count" => 11),
                           "oak" => array("title" => 'Дубові сходи',"class" => "sub_category", "link" => "", "items_count" => 8),
                          "pine" => array("title" => 'Сходи з сосни',"class" => "sub_category", "link" => "", "items_count" => 3)
                )
            ),
            "beds" => array("title" => 'Ліжка',"class" => "menu_category_item", "link" => "",
                "style" =>"",
                "sub"   => array(
                           "all" => array("title" => 'Всі ліжка',"class" => "active_sub_category", "link" => "", "items_count" => 11),
                           "oak" => array("title" => 'Дубові ліжка',"class" => "sub_category", "link" => "", "items_count" => 8),
                          "pine" => array("title" => 'Соснові ліжка',"class" => "sub_category", "link" => "", "items_count" => 3)
                )
            ),
            "tables" => array("title" => 'Столи',"class" => "menu_category_item", "link" => "",
                "style" =>"",
                "sub"   => array(
                           "all" => array("title" => 'Всі столи',"class" => "active_sub_category", "link" => "", "items_count" => 11),
                           "oak" => array("title" => 'Столи з дуба',"class" => "sub_category", "link" => "", "items_count" => 8),
                          "pine" => array("title" => 'Столи з сосни',"class" => "sub_category", "link" => "", "items_count" => 3)
                )
            ),
            "fires" => array("title" => 'Псевдокаміни',"class" => "menu_category_item", "link" => "",
                "style" =>"",
                "sub"   => array(
                           "all" => array("title" => 'Всі псевдокаміни',"class" => "active_sub_category", "link" => "", "items_count" => 11)
                )
            ),
        );

        $path_list = array(
            array("title" => "Головна", "link" => ""),
            array("title" => "Двері", "link" => ""),
        );

        $this->set_links_for_menu($category_list);

        $category_list[$category]['sub']['all']['class'] = 'active_sub_category';
        $category_list[$category]['style'] = 'display: inline-block';

        $category_list[$category]['class'] = explode(" ",$category_list[$category]['class'])[0]." menu_active_category_item";

        /*$item_info = array(
            "item_id" => '',
            "item_name" => '',
            "item_code" => '',
            "main_mid_photo_url" =>'',
            "main_lar_photo_url" =>'',
            "photo_list" => array("mid_url" => '',
                "lar_url" => ''),
            "feature_list" => array(
                "feature_name" =>'',
                "" =>''
            )
        );*/



        $item_exists = $this->model->item_exist($this->item_no);



        if(!$item_exists ){
            $this->view->display_404($category_list);
            exit();
        }


        $item_info = $this->model->get_item_info($this->item_no);

        $this->view->display_item($item_info,$category_list);

        //echo "<pre>";
        //var_dump($catalog_items);

    }
    public function set_default_args(){
        $this->item_no = 0;
    }
    public function parse_args($args){
        //echo "<br>".$args;

        $parsed_args = explode(';',$args);

        if($args ==""){
            $this->set_default_args();
            return;
        }


        if(isset($parsed_args[0])){
            $item_no = $parsed_args[0];
            $this->item_no = $item_no;
        }


    }
    public function form_catalog_url($category = null,$sub_category = null,$page = null, $sort = null, $page_size = null){
        $base_url = Host_config::$catalog_base_url;
        if(!isset($category)){
            return $base_url;
        }
        $result = $base_url.$category."/";
        $result = $result.$sub_category."/";


        $page_part = $this->form_url_args_element('page',$page);
        $result = $result.$page_part;

        $sort_part = $this->form_url_args_element('sort',$sort);
        $result = $result.$sort_part;

        $size_part = $this->form_url_args_element('pagesize',$page_size);
        $result = $result.$size_part;

        return $result;



    }




    public function log_to_console(){
        //echo '<script type="text/javascript">';
        //echo 'console.log("Hello World!")';
        //echo '</script>';
    }

    public  function set_links_for_menu(&$category_list){
        $base_url = Host_config::$catalog_base_url;
        foreach($category_list as $key => &$category){
            $category['link'] = $base_url .$key."/all/";
            foreach($category['sub'] as $sub_key => &$sub_category){
                $sub_category['link'] = $base_url .$key."/".$sub_key;
            }
        }

    }


} 